package org.linlinjava.litemall.wx.service;


import org.linlinjava.litemall.db.dao.FollowRelationMapper;
import org.linlinjava.litemall.db.domain.FollowRelation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class WxFollowService {
    @Autowired
    private FollowRelationMapper followRelationMapper;
    
    /**
     * 关注用户
     */
    public boolean follow(Long followerId, Long followingId, String followingName, String followingAvatar) {
        if (followerId.equals(followingId)) {
            return false; // 不能关注自己
        }
        
        FollowRelation existing = followRelationMapper.selectByFollowerAndFollowing(
            followerId, followingId);
        if (existing != null) {
            return false; // 已经关注过了
        }
        
        FollowRelation relation = new FollowRelation();
        relation.setFollowerId(followerId);
        relation.setFollowingId(followingId);
        relation.setFollowingName(followingName);
        relation.setFollowingAvatar(followingAvatar);
        
        return followRelationMapper.insert(relation) > 0;
    }
    
    /**
     * 取消关注
     */
    public boolean unfollow(Long followerId, Long followingId) {
        return followRelationMapper.deleteByFollowerAndFollowing(followerId, followingId) > 0;
    }
    
    /**
     * 检查是否已关注
     */
    public boolean isFollowing(Long followerId, Long followingId) {
        return followRelationMapper.selectByFollowerAndFollowing(followerId, followingId) != null;
    }
    
    /**
     * 获取关注列表
     */
    public List<FollowRelation> getFollowings(Long followerId) {
        return followRelationMapper.selectFollowingsByFollowerId(followerId);
    }
    
    /**
     * 获取粉丝列表
     */
    public List<FollowRelation> getFollowers(Long followingId) {
        return followRelationMapper.selectFollowersByFollowingId(followingId);
    }
} 